package com.fxpc.hddc.hdc100.jpa.dao.pg.impl;


import com.fxpc.common.vo.DivisionParamVo;
import com.fxpc.hddc.hdc100.dto.pg.entity.FxqhHdc100BfEntity;
import com.fxpc.hddc.hdc100.jpa.dao.pg.FxqhHdc100PgNativeDao;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * @author zyb
 * @date 2020-11-30
 */
@Repository
public class FxqhHdc100PgNativeDaoImpl implements FxqhHdc100PgNativeDao {
    @PersistenceContext(unitName = "secondaryPersistenceUnit")
    private EntityManager em;
    private static final Logger log = LoggerFactory.getLogger(FxqhHdc100PgNativeDaoImpl.class);

    @Override
    public List<FxqhHdc100BfEntity> findPgOneDayDataByCreateTime1(LocalDate nowDay) {
        StringBuilder sql = new StringBuilder("select id,province,city,area,create_time from fxqh_hdc100 ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        whereSql.append(" create_time>=CURRENT_DATE");
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxqhHdc100BfEntity.class);
//        query.setParameter("start", start);
//        query.setParameter("end", end);
        List<FxqhHdc100BfEntity> list = query.getResultList();
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list;
    }

    @Override
    public List<FxqhHdc100BfEntity> findPgYhDisastersByDivision(DivisionParamVo divisionParamVo) {
        StringBuilder sql = new StringBuilder("select * from fxqh_hdc100 ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (Objects.isNull(divisionParamVo)) {
            return null;
        }
        String province = divisionParamVo.getProvince();
        String city = divisionParamVo.getCity();
        String area = divisionParamVo.getArea();
        if (StringUtils.isNotEmpty(province)) {
            whereSql.append(" and province = :province");
        }
        if (StringUtils.isNotEmpty(city)) {
            whereSql.append(" and city = :city");
        }
        if (StringUtils.isNotEmpty(area)) {
            whereSql.append(" and area = :area");
        }
        whereSql.append(" and is_valid = '1'");
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxqhHdc100BfEntity.class);
        if (StringUtils.isNotEmpty(province)) {
            query.setParameter("province", province);
        }
        if (StringUtils.isNotEmpty(city)) {
            query.setParameter("city", city);
        }
        if (StringUtils.isNotEmpty(area)) {
            query.setParameter("area", area);
        }
        List<FxqhHdc100BfEntity> list = query.getResultList();
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list;
    }
}
